package com.util;

import javax.servlet.ServletContext;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;

@WebListener
public class LoginUserNumberListener implements HttpSessionAttributeListener {

	private int num; // 统计登录人数

	public LoginUserNumberListener() {
	}

	
	public void attributeAdded(HttpSessionBindingEvent event) {
		ServletContext context = event.getSession().getServletContext();
		if ("loginUser".equals(event.getName())){
			//会话创建时，人数+1
			num++;
			context.setAttribute("loginUserNum", num);
		}
	}

	
	public void attributeRemoved(HttpSessionBindingEvent event) {
		ServletContext context = event.getSession().getServletContext();
		if ("loginUser".equals(event.getName())){
			
			num--;
			context.setAttribute("loginUserNum", num);
		}
	}


	public void attributeReplaced(HttpSessionBindingEvent event) {
		
	}

}
